
package com.ray.tcc.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.table.entity.Button;
import com.ray.tcc.common.page.CommonPager;
import com.ray.tcc.common.page.PageParameter;
import com.ray.tcc.dto.CompensationDTO;
import com.ray.tcc.query.CompensationQuery;
import com.ray.tcc.service.CompensationService;
import com.ray.tcc.util.DataGridUtil;
import com.ray.tcc.vo.TccCompensationVO;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

/**
 * <p>Description: .</p>
 * 事务恢复controller
 *
 * @author ray
 * @version 1.0
 * @date 2017/10/18 10:31
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/tcc/compensate")
public class CompensationController extends WebController{

	 @Autowired
     CompensationService compensationService;
	 @Autowired
	 UcenterApiService ucenterApiService;

    @Value("${compensation.retry.max}")
    private Integer recoverRetryMax;

    
    
    @RequestMapping(value="view")
	@RequiresPermissions("tcc:compensate:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
		modelAndView.setViewName("tcc/compensate/view");
		return modelAndView;
		
	}
	
	/*
	 * 修改页面
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("tcc:compensate:edit")
	@ResponseBody
	public ModelAndView updateView(String menuId,TccCompensationVO params,String applicationName,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
		modelAndView.addObject("record",params );
		modelAndView.addObject("applicationName",applicationName );
		modelAndView.setViewName("tcc/compensate/edit");
		return modelAndView;
		   
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value = "edit")
	@RequiresPermissions("tcc:compensate:edit")
	@ResponseBody
	public ResponseEntity<Result<Object>> update(CompensationDTO params,HttpServletRequest request, HttpServletResponse respose){
		
		 if (recoverRetryMax < params.getRetry()) {
        	 return doError(String.format("尝试次数不能大于：%d", recoverRetryMax));
        }
        final Boolean success = compensationService.updateRetry(params.getId(),
        		params.getRetry(), params.getApplicationName());
        if(success){
        	 return doSuccess("修改成功");
        }
       return doError("修改失败");
		
	}
	
	

	/**
	 * 应用删除页面
	 */
	@RequestMapping(value = "delete")
	@RequiresPermissions("tcc:compensate:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> delete(CompensationDTO params, HttpServletRequest request, HttpServletResponse respose){
		    List<String> ids = new ArrayList<String>();
		    ids.add(params.getId());
		    final Boolean success = compensationService.batchRemove(ids, params.getApplicationName());
	        if(success)return doSuccess("删除成功");
	        return doError("删除失败");
	}
	/**
	 * 应用查询页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "query")
	@RequiresPermissions("tcc:compensate:query")
	@ResponseBody
    public ResponseEntity<Map<String,Object>> query(CompensationQuery recoverQuery, HttpServletRequest request, HttpServletResponse respose){
		PageDto<TccCompensationVO> page= this.createJQPage(request);
		recoverQuery.setPageParameter(new PageParameter(page.getPage(), page.getPageSize()));
		final CommonPager<TccCompensationVO> pager = compensationService.listByPage(recoverQuery);
		page.setRecords(pager.getDataList());
		if(ObjectUtil.isNotNull(pager.getPage())){
			page.setTotal(pager.getPage().getTotalCount());
		}
		
		return doSuccess(DataGridUtil.dataConvert(page));

	}	
	

}
